# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jeff Bilyk <jbilyk at gmail>
# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Maintainer: Kevin Daudt <kdaudt@alpinelinux.org>
pkgname=zabbix
pkgver=6.4.14
pkgrel=0
pkgdesc="Enterprise-class open source distributed monitoring"
url="https://www.zabbix.com/"
arch="all"
license="GPL-2.0-or-later"
depends="fping"
_php=php82
_php_depends="$_php
	$_php-bcmath
	$_php-ctype
	$_php-curl
	$_php-fileinfo
	$_php-gd
	$_php-gettext
	$_php-iconv
	$_php-mbstring
	$_php-openssl
	$_php-session
	$_php-simplexml
	$_php-sockets
	$_php-xmlreader
	$_php-xmlwriter
	"
makedepends="
	libxml2-dev
	sqlite-dev
	autoconf
	automake
	curl-dev
	go
	libevent-dev
	libpq-dev
	libssh2-dev
	mariadb-connector-c-dev
	net-snmp-dev
	openipmi-dev
	openssl-dev>3
	pcre2-dev
	unixodbc-dev
	$_php_depends
	"
install="$pkgname.pre-install"
pkgusers="zabbix"
pkggroups="zabbix"
options="!check" # no tests available
subpackages="
	$pkgname-agent
	$pkgname-agent-openrc:agent_openrc
	$pkgname-agent2
	$pkgname-agent2-openrc:agent2_openrc
	$pkgname-doc
	$pkgname-mysql
	$pkgname-openrc
	$pkgname-pgsql
	$pkgname-setup::noarch
	$pkgname-sqlite
	$pkgname-utils
	$pkgname-webif::noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/zabbix/zabbix/archive/${pkgver/_/}.tar.gz
	zabbix-server.initd
	zabbix-server.confd
	zabbix-agentd.initd
	zabbix-agent2.initd
	zabbix-proxy.initd

	fix-msghdr.patch
	musl-fix-includes.patch
	zabbix_server.conf.patch
	zabbix_agent2.conf.patch
	ui-services-fix-php-80.patch
	"
#builddir="$srcdir/$pkgname-${pkgver/_/}"

# secfixes:
#   5.4.9-r1:
#     - CVE-2022-22704
#   5.2.6-r0:
#     - CVE-2021-27927
#   3.0.4-r0:
#     - CVE-2016-9140

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export GOFLAGS="$GOFLAGS -modcacherw"

prepare() {
	default_prepare
	autoreconf -fvi

	# Fix config file locations
	for file in server agentd proxy; do
		sed -i "$builddir"/conf/zabbix_$file.conf \
			-e 's|SNMPTrapperFile=/tmp|SNMPTrapperFile=/var/log/zabbix|' \
			-e 's|PidFile=/tmp|PidFile=/var/run/zabbix|' \
			-e 's|LogFile=/tmp|LogFile=/var/log/zabbix|'
	done
}

build() {
	export CFLAGS="$CFLAGS -D_LARGEFILE64_SOURCE"

	# set default configure flags
	_configure="--prefix=/usr \
			--sysconfdir=/etc/zabbix \
			--mandir=/usr/share/man \
			--infodir=/usr/share/info \
			--enable-agent \
			--enable-agent2 \
			--enable-proxy \
			--enable-ipv6 \
			--with-net-snmp \
			--with-libcurl \
			--with-libxml2 \
			--with-openipmi \
			--with-unixodbc \
			--with-ssh2 \
			--with-openssl \
			--with-libpcre2
			"

	export GOPATH="$builddir"
	# pass --export-dynamic to the linker
	# to make zabbix loadable modules work
	LDFLAGS="$LDFLAGS -Wl,--export-dynamic"
	# we run build for each db type
	# make sure prepare is same for each db
	for db in postgresql mysql sqlite3; do
		cd "$srcdir"
		msg "Building for $db"
		cp -r "$builddir" "$builddir-$db"
		cd "$builddir-$db"
		case $db in
			sqlite3)
				./configure \
					--build=$CBUILD \
					--host=$CHOST \
					--with-$db \
					$_configure
			;;
			*)
				./configure \
					--build=$CBUILD \
					--host=$CHOST \
					--enable-server \
					--with-$db \
					$_configure
			;;
		esac
		make AGENT_GOTAGS="pcre2 libsqlite3"
		make dbschema
	done
}

package() {
	# doing manual install
	components="agentd agent2 proxy server";

	for i in $components; do
		install -D -m755 "$builddir"/man/zabbix_$i.man \
			"$pkgdir"/usr/share/man/man8/zabbix_$i.8
	done
	for i in get sender; do
		install -D -m755 "$builddir"/man/zabbix_$i.man \
			"$pkgdir"/usr/share/man/man1/zabbix_$i.1
	done
	install -d -m0750 -o zabbix -g zabbix \
		"$pkgdir"/var/run/zabbix "$pkgdir"/var/log/zabbix
	install -D -m0644 "$builddir"/conf/zabbix_server.conf \
		"$pkgdir"/etc/zabbix/zabbix_server.conf
	for i in server proxy; do
		install -D -m0755 "$srcdir"/zabbix-$i.initd \
			"$pkgdir"/etc/init.d/zabbix-$i
	done
	install -D -m0644 "$srcdir"/zabbix-server.confd \
		"$pkgdir"/etc/conf.d/zabbix-server

	install -Dm755 "$builddir-postgresql"/src/go/bin/zabbix_agent2 \
		-t "$pkgdir"/usr/sbin/
	install -Dm755 "$builddir-postgresql"/src/zabbix_agent/zabbix_agentd \
		-t "$pkgdir"/usr/sbin
}

setup() {
	pkgdesc="Zabbix images and sql files"
	depends=
	mkdir -p "$subpkgdir"/usr/share/zabbix
	cp -r "$builddir-postgresql"/database "$subpkgdir"/usr/share/zabbix/
	find "$subpkgdir"/usr/share/zabbix -name 'Makefile*' -delete
}

_do_db() {
	pkgdesc="Zabbix server with $1 database support"
	depends="$pkgname $2"
	local i=
	mkdir -p "$subpkgdir"/usr/sbin
	if [ "$1" != "sqlite3" ]; then
		cp "$builddir-$1"/src/zabbix_server/zabbix_server \
			"$subpkgdir"/usr/sbin
	fi
	cp "$builddir-$1"/src/zabbix_proxy/zabbix_proxy	\
		"$subpkgdir"/usr/sbin
	install	-D -m0644 "$builddir"/conf/zabbix_proxy.conf \
		"$pkgdir"/etc/zabbix/zabbix_proxy.conf
	cd "$builddir"
	for i in upgrades/dbpatches/*/"$1"; do
		[ -e $i ] || continue
		mkdir -p "$subpkgdir"/usr/share/zabbix/${i%/*}
		cp "$i" "$subpkgdir"/usr/share/zabbix/$i
	done
	return 0
}

pgsql() { _do_db postgresql "!$pkgname-mysql !$pkgname-sqlite";}
mysql() { _do_db mysql "!$pkgname-pgsql !$pkgname-sqlite";}
sqlite() { _do_db sqlite3 "!$pkgname-pgsql !$pkgname-mysql";}

utils() {
	pkgdesc="Zabbix client utilities"
	depends=

	install -Dm0755 \
		"$builddir-postgresql"/src/zabbix_get/zabbix_get \
		"$builddir-postgresql"/src/zabbix_sender/zabbix_sender \
		"$builddir-postgresql"/src/zabbix_js/zabbix_js \
		-t "$subpkgdir"/usr/bin
}

webif() {
	pkgdesc="Zabbix web-interface"
	depends="$_php_depends"
	_wwwdir="$subpkgdir"/usr/share/webapps/zabbix
	mkdir -p $_wwwdir
	cp -r "$builddir"/ui/* "$_wwwdir"
}

agent() {
	pkgdesc="Zabbix Network Monitoring Agent"
	depends=
	install="$subpkgname.pre-install"

	amove usr/sbin/zabbix_agentd

	install -d -m0750 -o zabbix -g zabbix \
		"$subpkgdir"/var/run/zabbix "$subpkgdir"/var/log/zabbix

	install -D -m0644 "$builddir"/conf/zabbix_agentd.conf \
		"$subpkgdir"/etc/zabbix/zabbix_agentd.conf
}

agent_openrc() {
	pkgdesc="Zabbix Network Monitoring Agent (OpenRC init scripts)"
	depends=openrc
	install_if="openrc $pkgname-agent=$pkgver-r$pkgrel"

	install -D -m0755 "$srcdir"/zabbix-agentd.initd \
		"$subpkgdir"/etc/init.d/zabbix-agentd
}

agent2() {
	pkgdesc="Zabbix Network Monitoring Agent (version 2)"
	depends=
	install="$subpkgname.pre-install"

	amove usr/sbin/zabbix_agent2

	install -d -m0750 -o zabbix -g zabbix \
		"$subpkgdir"/var/run/zabbix "$subpkgdir"/var/log/zabbix

	install -D -m0644 "$builddir"/src/go/conf/zabbix_agent2.conf \
		"$subpkgdir"/etc/zabbix/zabbix_agent2.conf

	install -D -m0644 "$builddir"/src/go/conf/zabbix_agent2.d/plugins.d/* \
		-t "$subpkgdir"/etc/zabbix/zabbix_agent2.d/plugins.d/
}

agent2_openrc() {
	pkgdesc="Zabbix Network Monitoring Agent 2 (OpenRC init scripts)"
	depends=openrc
	install_if="openrc $pkgname-agent2=$pkgver-r$pkgrel"

	install -D -m0755 "$srcdir"/zabbix-agent2.initd \
		"$subpkgdir"/etc/init.d/zabbix-agent2
}

sha512sums="
cbfa6313170dd0a0395535227f57f515d4638d9110f128fdd60ccf16519e75ff79a8a4af98fdb5c78cff1f95cf09c1ef3cb270b733d0004c8e75da330af59568  zabbix-6.4.14.tar.gz
9998ee172a28002d98bacc3f76038ff52b8cf2b206e101418d76b4ca3de94afaf92cb4f7a6235ecf177f74beb9dd3ea1f3983c4f164b4f60bb601acba65aa175  zabbix-server.initd
9c06527bf653c40585fa7eeb3f7a0b2fc454031d24cd0d1633aed87b78a681c5227a193c5b9fcfcea0839135874e27ba7dd9b198573f905f680a2856f79e9512  zabbix-server.confd
7beca0fc6e254c1692e0e74deb9eb3d06ac78f5f6b08f3ab0491861e11e09f00f57bb4d22e11517dab86456e87bd13110805bfb38a715d2f1e68549937b29c76  zabbix-agentd.initd
fed1e11ca4d97ad348308d02dff800598748c427bc427c3d52b9bca6a9269904c83ae6be08bacccc65d3d71c6ce50f895782d0650e27244bd2bdbe4ecb807e78  zabbix-agent2.initd
a26e7ac422ff60a4b8eed3603022c3a1bde640870bb9286ab061c3cb5c2fd7e91ddb317cb3d1cf61034adda0a080fc212ad416c9e2853a1deb03c5279753f4e2  zabbix-proxy.initd
a519b2bced34fec74a4db0fbfdbcca832fc71b9268e30dbd3b7a9e0e0a4bce310229f3fce35f990573c8980ae60ef3cca0d54ee22d22430532e172d15c68e102  fix-msghdr.patch
442f7dcc47129955618f8c43ff0d706dcad582725e37256477992ee10c18d93484c4180a815437ce736dbcf880cbf08cf9642b7a07e9d6c2f6aa994712ea5286  musl-fix-includes.patch
183e2d99800d8eebb932cb31c5a3e8742ce6a15e87aec50dc49d9c20b35840eb7ede9920aa62d2c7a5e3f584f5c3df398aa08e93f98fa537a3ba6db63b472a1f  zabbix_server.conf.patch
35ea336a8f3e0bdcec4a0cde01fcd9edfa20a94f29ca3a1905aa8a60938f34101ef8d9bdebf744a7ac53ee3b1ece4c3491def52c6bd8394ce6fed88ca93586b1  zabbix_agent2.conf.patch
c4a0ae137f70bf477a74a43424a66a759f8828907d441213fbe5cba6e6865b3741ae625be534698423853cfaaeed9823dd537b7aa1689e2992a36ff8a5a24127  ui-services-fix-php-80.patch
"
